package com.sina.shop.manage.service.collect;

import com.sina.shop.core.exception.client.DataException;
import com.sina.shop.core.exception.server.CreateException;
import com.sina.shop.core.exception.server.ResourceExistsException;
import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.admin.Admin;
import com.sina.shop.domain.collect.CollectBusiness;
import com.sina.shop.manage.dao.admin.AdminManageDao;
import com.sina.shop.manage.dao.collect.CollectBusinessManageDao;
import com.sina.shop.service.ShopDataSourceService;
import com.sina.shop.util.Assert;
import com.sina.shop.util.ResultCode;
import com.sina.shop.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.sina.shop.service.ServiceConstant.SHOP_TX;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-01-16-9:46
 */
@Service("collectBusinessManageService")
public class CollectBusinessManageServiceImpl extends ShopDataSourceService implements CollectBusinessManageService {

    @Autowired
    @Qualifier("collectBusinessManageDao")
    private CollectBusinessManageDao collectBusinessManageDao;

    @Autowired
    @Qualifier("adminManageDao")
    private AdminManageDao adminManageDao;

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public CollectBusiness getCollectBusinessById(Long id) {
        return collectBusinessManageDao.get(CollectBusiness.class, id);
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public int deleteCollectBusiness(Long id) throws DataException, UpdateException {
        CollectBusiness collectBusiness = collectBusinessManageDao.get(CollectBusiness.class, id);
        Assert.assertNotNull(collectBusiness, "CollectBusiness[id=%s] not exists", collectBusiness.getId());

        try {
            return collectBusinessManageDao.deleteCollectBusiness(id);
        } catch (DataAccessException e) {
            LOG.error(" {} delete CollectBusiness error.", CollectBusiness.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.READ_COMMITTED)
    public CollectBusiness saveCollectBusiness(CollectBusiness collectBusiness) throws CreateException, ResourceExistsException, DataException {
        try {
            //如果已收藏过，则抛出异常
            if (collectBusiness.getBusiness() != null && collectBusiness.getAdmin() != null) {
                if (collectBusinessManageDao.getCollectBusinessByAidAndBid(
                        collectBusiness.getAdmin().getId(),
                        collectBusiness.getBusiness().getId()
                ) != null) {
                    throw new DataAccessResourceFailureException("收藏失败，你已收藏过！！");
                }
            }

            collectBusinessManageDao.save(collectBusiness);
            collectBusinessManageDao.flush();
            return collectBusiness;
        } catch (DataAccessException e) {
            LOG.error(" {} save CollectBusiness error.", CollectBusiness.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public CollectBusiness updateCollectBusiness(CollectBusiness collectBusiness) throws UpdateException, DataException {
        return null;
    }

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public List<Map<String, Object>> getCollectBusinessList(CollectBusiness collectBusiness, Long[] count) {

        Admin admin = null;
        if (collectBusiness.getAdmin() != null && !StringUtils.isEmpty(collectBusiness.getAdmin().getAccount())) {
            admin = adminManageDao.getAdmin(collectBusiness.getAdmin().getAccount());
            if (admin == null) {
                admin = new Admin();
            }
        }
        collectBusiness.setAdmin(admin);

        if (count != null && count.length > 0) {
            count[0] = collectBusinessManageDao.getCollectBusinessCount(collectBusiness);
        }
        return collectBusinessManageDao.getCollectBusinessList(collectBusiness);
    }

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public CollectBusiness getCollectBusinessByAidAndBid(Long aid, Long bid) {
        return collectBusinessManageDao.getCollectBusinessByAidAndBid(aid, bid);
    }
}
